<?php

namespace app\beautiful\model\partner;

use app\beautiful\model\PartnerModel;
use app\common\model\CommonModel;
use think\facade\Db;


class PartnerBonusModel extends CommonModel{

    protected  $name = 'beautiful_partner_bonus';

    protected  $pk   = 'partner_bonus_id';
    
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }

    public function partner()

    {

        return $this->hasOne(PartnerModel::class,'partner_id','partner_id');

    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
    public static function ulist($where,$order,$page=1,$limit=10,$join_task_bonus_role_id=[],$task_status=0){
        $query = Db::name('beautiful_partner_bonus')->alias('a')
            ->join('shop b','a.shop_id=b.shop_id')
            ->join('shop_detail d','a.shop_id=d.shop_id')
            ->join('beautiful_partner p','a.partner_id=p.partner_id')
            ->join('beautiful_member m','p.member_id=m.member_id')
//            ->where(['a.status'=>1])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['partner_id']),function ($query) use ($where){
                $query->where(['a.partner_id'=>$where['partner_id']]);
            })
            ->when(!empty($where['bonus_id']),function ($query) use ($where){
                $query->where(['a.bonus_id'=>$where['bonus_id']]);
            })
            ->when(!empty($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->when(!empty($where['end_time']),function ($query) use ($where){
                $query->where('a.end_time','<',$where['end_time']);
            })
            ->when(!empty($where['search']),function ($query) use ($where){
                $query->where('m.nick_name|m.mobile','like','%'.$where['search'].'%');
            })
            ->when(isset($where['is_delete']),function ($query) use ($where){
                $query->where('p.is_delete','=',$where['is_delete']);
            })
            ->field("a.*,a.cousume_bonus_ratio+a.toker_bonus_ratio+a.promotion_bonus_ratio	 as total_ratio,b.name as shop_name,d.logo,p.role_id,p.name,m.nick_name,m.member_num,m.mobile");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
//        var_dump(Db::getLastSql());exit();
        if(!empty($list)){
            foreach ($list as &$v){
                $where=[];
                $where['shop_id']=$v['shop_id'];
                $where['start_time']=strtotime($v['start_time']);
                $where['end_time']=strtotime($v['end_time']);
                $reward_money=partner_reward_money($where);
                $v['all_reward_money']=$reward_money;
                $v['task_detail']=json_decode($v['task_detail'],true);
                $v['is_in_task_bonus'] = ($join_task_bonus_role_id && in_array($v['role_id'],$join_task_bonus_role_id))?1:0;
                $v['task_status'] = $task_status;
            }
        }
        return compact('count','list');
    }
    public function getById($where,$field='*')
    {
        $list = $this->where($where)->field($field)->json(['task_detail'],true)->find();
        if($list) $list = $list->toArray();

        return $list;
    }
}

